<template>
  <el-dialog
    :visible.sync="isShow"
    title="为【人事经理】分配权限"
    @close="closeEvent"
  >
    <el-tree
      ref="tree"
      check-strictly
      show-checkbox
      :data="treeData"
      node-key="id"
      :props="{ label: 'name' }"
    />
    <template #footer>
      <div style="text-align: center">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permission'
import { sysRoleGet, sysRoleAssignPrem } from '@/api/setting'
export default {
  data() {
    return {
      isShow: false,
      treeData: [],
      id: ''
      //   permIds: []
    }
  },
  methods: {
    // 获取所有权限点
    async getData() {
      const res = await sysPermission()
      this.treeData = this.changeTree(res.data, '0')
      //   console.log(this.treeData)
    },
    // 树形数据转化
    changeTree(arr, str) {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changeTree(arr, item.id)
          return true
        }
      })
    },
    // 拿到勾选项
    // checkKeys() {
    //   this.$refs.tree.getCheckedKeys()
    // }
    // 获取角色详情
    async getList(id) {
      this.id = id
      const res = await sysRoleGet(id)
      //   this.permIds = res.data.permIds
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      //   console.log(this.permIds)
    },
    // 关闭弹框 清空以勾选项
    closeEvent() {
      this.$refs.tree.setCheckedKeys([])
    },
    // 点击确定调用接口
    async submit() {
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配权限成功')
      this.isShow = false
    }
  }
}
</script>

<style></style>
